package cn.edu.gzu.controller;

import cn.edu.gzu.common.Result;
import cn.edu.gzu.common.ResultCode;
import cn.edu.gzu.entity.HistoryEntity;
import cn.edu.gzu.entity.VideoTotalEntity;
import cn.edu.gzu.feign.VideoFeign;
import cn.edu.gzu.service.HistoryService;
import cn.edu.gzu.service.VideoTotalService;
import cn.edu.gzu.vo.HistoryVo;
import cn.edu.gzu.vo.TotalPageVo;
import cn.edu.gzu.vo.VideoVo;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/videoTotal")
public class VideoTotalController {

    @Resource(name = "videoTotalService")
    private VideoTotalService videoTotalService;

    @Resource(name = "historyService")
    private HistoryService historyService;

    @Resource
    private VideoFeign videoFeign;

    @PostMapping("/page")
    public JSONObject getPage(@RequestBody TotalPageVo totalPageVo){
        System.out.println("用户id："+totalPageVo.getUserId());
        //保存历史记录
        if (!Objects.equals(totalPageVo.getQuery_data(), "")) {
            QueryWrapper<HistoryEntity> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("user_id",totalPageVo.getUserId());
            queryWrapper1.eq("query_keyword",totalPageVo.getQuery_data());
            HistoryEntity has_history = historyService.getOne(queryWrapper1);
            if (has_history == null) {
                HistoryEntity history = new HistoryEntity();
                history.setUserId(totalPageVo.getUserId());
                history.setQueryKeyword(totalPageVo.getQuery_data());
                historyService.save(history);
            }
        }
        LambdaQueryWrapper<VideoTotalEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(VideoTotalEntity::getId,VideoTotalEntity::getTopic,VideoTotalEntity::getCreateTime,VideoTotalEntity::getCoverUrl)//只返id,回标题,时间和请求地址
                .like(VideoTotalEntity::getTopic, totalPageVo.getQuery_data())
                .orderByAsc(VideoTotalEntity::getId);
        if (totalPageVo.getSort_type() != 0) {
            queryWrapper.eq(VideoTotalEntity::getSortType,totalPageVo.getSort_type());
        }
        IPage<VideoTotalEntity> page = videoTotalService.page(new Page<>(totalPageVo.getPage(), totalPageVo.getSize()), queryWrapper);
        if (totalPageVo.getPage() > page.getPages() && page.getTotal() != 0) {
            return Result.failure(ResultCode.FAILURE_QUERY_PAGE_END_ERROR,page);
        }
        if (page.getTotal() < 1) {
            return Result.failure(ResultCode.FAILURE_QUERY_VIDEO_ERROR,page);
        }

        return Result.success("刷新成功！",page);
    }

    @PostMapping("/info")
    public JSONObject getInfo(@RequestParam("totalId") long totalId){
        VideoTotalEntity videoTotal = videoTotalService.getById(totalId);
        List<Object> videos = videoFeign.getVideoInfo(videoTotal.getId());
        if (videos.size() < 1) {
            return Result.failure(ResultCode.FAILURE_QUERY_VIDEO_ERROR);
        }
        VideoVo videoVo = new VideoVo();
        videoVo.setTotal_id(videoTotal.getId());
        videoVo.setTotal_topic(videoTotal.getTopic());
        videoVo.setTotal_url(videoTotal.getCoverUrl());
        videoVo.setVideo(videos);
        return Result.success("获取成功！",videoVo);
    }

    @GetMapping("/history")
    public JSONObject getHistory(@RequestParam("userId")long id){
        System.out.println("userid:"+id);
        QueryWrapper<HistoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        queryWrapper.last("limit 20");
        List<HistoryEntity> list = historyService.list(queryWrapper);
        if (list.size() < 1) {
            return Result.failure(ResultCode.FAILURE);
        }
        HistoryVo historyVo = new HistoryVo();
        historyVo.setHistory(list);
        return Result.success("获取历史记录成功",historyVo);
    }
}
